package com.lpy.book.JFrame;

import com.lpy.book.dao.BookDao;
import com.lpy.book.model.Book;
import com.lpy.book.model.BookType;
import com.lpy.book.model.DbUtil;
import com.lpy.book.utils.ToolUtil;
import javafx.scene.control.ComboBox;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.sql.Connection;

public class AdminBookEdit {

    JFrame jf;
    JTextField textField;
    JTextField textField_1;
    JTextField textField_2;
    JTextField textField_3;
    JTextField textField_4;
    JTextField textField_5;
    JTextField textField_6;
    JTextField textField_7;

    ToolUtil toolUtil = new ToolUtil();


    public AdminBookEdit() {
        jf = new JFrame();
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setTitle("111");
        jf.setSize(600, 800);
        jf.setLocation(400, 200);
        jf.setVisible(true);

        JPanel panel_1 = new JPanel();
        panel_1.setLayout(null);
        panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u4E66\u7C4D\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(255, 0, 0)));
        panel_1.setBounds(20, 105, 541, 195);



        /*做一个表头栏数据  一维数组 * */
        String[] title={"编号", "书名", "类别", "作者", "价格", "库存", "状态" };
        /*具体的各栏行记录 先用空的二维数组占位*/
        String[][] dates={};
        /*然后实例化 上面2个控件对象*/
        DefaultTableModel model;
        model = new DefaultTableModel(dates, title);
        JTable table = new JTable(model);
        putDates(new Book());//获取数据库数据放置table中
        JScrollPane jscrollpane = new JScrollPane();
        jscrollpane.setBounds(20, 22, 496, 154);
        jscrollpane.setViewportView(table);
        panel_1.add(jscrollpane);

        jf.getContentPane().add(panel_1);
        jf.getContentPane().add(panel_1);

        JPanel panel_2 = new JPanel();
        panel_2.setBounds(20, 310, 541, 292);
        jf.getContentPane().add(panel_2);
        panel_2.setLayout(null);
        JLabel label = new JLabel("编号：");
        label.setFont(new Font("幼圆", Font.BOLD, 14));
        label.setBounds(58, 10, 45, 27);
        panel_2.add(label);
        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(101, 10, 129, 27);
        panel_2.add(textField_1);
        JLabel label_1 = new JLabel("书名：");
        label_1.setFont(new Font("幼圆", Font.BOLD, 14));
        label_1.setBounds(294, 10, 45, 27);
        panel_2.add(label_1);
        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(338, 10, 128, 27);
        panel_2.add(textField_2);
        JLabel label_2 = new JLabel("作者：");
        label_2.setFont(new Font("幼圆", Font.BOLD, 14));
        label_2.setBounds(58, 58, 45, 27);
        panel_2.add(label_2);
        textField_3 = new JTextField();
        textField_3.setColumns(10);
        textField_3.setBounds(101, 58, 129, 27);
        panel_2.add(textField_3);
        JLabel label_3 = new JLabel("价格：");
        label_3.setFont(new Font("幼圆", Font.BOLD, 14));
        label_3.setBounds(58, 104, 45, 27);
        panel_2.add(label_3);
        textField_4 = new JTextField();
        textField_4.setColumns(10);
        textField_4.setBounds(101, 104, 129, 27);
        panel_2.add(textField_4);
        JLabel label_4 = new JLabel("出版：");
        label_4.setFont(new Font("幼圆", Font.BOLD, 14));
        label_4.setBounds(294, 58, 45, 27);
        panel_2.add(label_4);
        textField_5 = new JTextField();
        textField_5.setColumns(10);
        textField_5.setBounds(337, 58, 129, 27);
        panel_2.add(textField_5);
        JLabel label_5 = new JLabel("类别：");
        label_5.setFont(new Font("幼圆", Font.BOLD, 14));
        label_5.setBounds(58, 189, 45, 27);
        panel_2.add(label_5);
        JComboBox comboBox_1;
        comboBox_1 = new JComboBox();
        comboBox_1.setBounds(102, 190, 128, 26);
        //获取类别
        getBookType();
        panel_2.add(comboBox_1);
        JLabel label_6 = new JLabel("库存：");
        label_6.setFont(new Font("幼圆", Font.BOLD, 14));
        label_6.setBounds(294, 104, 45, 27);
        panel_2.add(label_6);
        textField_6 = new JTextField();
        textField_6.setColumns(10);
        textField_6.setBounds(337, 104, 129, 27);
        panel_2.add(textField_6);
        JLabel label_7 = new JLabel("描述：");
        label_7.setFont(new Font("幼圆", Font.BOLD, 14));
        label_7.setBounds(58, 152, 45, 27);
        panel_2.add(label_7);
        textField_7 = new JTextField();
        textField_7.setColumns(10);
        textField_7.setBounds(101, 152, 365, 27);
        panel_2.add(textField_7);
        JLabel label_8 = new JLabel("状态：");
        label_8.setFont(new Font("幼圆", Font.BOLD, 14));
        label_8.setBounds(294, 190, 45, 27);
        panel_2.add(label_8);
        JComboBox comboBox_2 = new JComboBox();
        comboBox_2.setBounds(338, 191, 128, 26);
        comboBox_2.addItem("上架");
        comboBox_2.addItem("下架");
        panel_2.add(comboBox_2);
        JButton btnNewButton_1 = new JButton("修改");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String bookName = textField_2.getText();
                String author = textField_3.getText();
                String publish = textField_5.getText();
                String priceStr = textField_4.getText();
                String numberStr = textField_6.getText();
                String remark = textField_7.getText();
                String bookId = textField_1.getText();
                if (toolUtil.isEmpty(bookId) || toolUtil.isEmpty(bookName)
                        || toolUtil.isEmpty(author) || toolUtil.isEmpty(publish)
                        || toolUtil.isEmpty(priceStr) ||toolUtil.isEmpty(numberStr)
                        || toolUtil.isEmpty(remark)) {
                    JOptionPane.showMessageDialog(null, "请输入相关内容");
                    return;
                }
                BookType selectedItem = (BookType) comboBox_1.getSelectedItem();
                Integer typeId = selectedItem.getTypeId();
                int index = comboBox_2.getSelectedIndex();
                int number;
                double price;
                try {
                    number = Integer.parseInt(numberStr);
                    price = new BigDecimal(priceStr).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
                } catch (Exception e1) {
                    JOptionPane.showMessageDialog(null, "参数错误");
                    return;
                }
                Book book = new Book();
                book.setBookId(Integer.parseInt(bookId));
                book.setBookName(bookName);
                book.setAuthor(author);
                book.setBookTypeId(typeId);
                book.setNumber(number);
                book.setPrice(price);
                book.setPublish(publish);
                book.setRemark(remark);
                book.setStatus(1);
                if (index == 0) {
                    book.setStatus(1);
                } else if (index == 1) {
                    book.setStatus(2);
                }
                Connection con = null;
                try {
                    con = DbUtil.getConnection();
                    int i = BookDao.update(con, book);
                    if (i == 1) {
                        JOptionPane.showMessageDialog(null, "修改成功");
                    } else {
                        JOptionPane.showMessageDialog(null, "修改失败");
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                    JOptionPane.showMessageDialog(null, "修改异常");
                }finally{
                    try {
                        DbUtil.close((com.mysql.jdbc.Connection) con, null, null);
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
                putDates(new Book());
            }


        });

    }

    private void getBookType() {
    }

    private void putDates(Book book) {

    }











    public static void main(String[] args) {
        AdminBookEdit adminBookEdit = new AdminBookEdit();
    }

}
